*************************************************************************************************************
	    ** Commodity-Country level analysis using China's aggregate data: extended to Dec 2019 **
*************************************************************************************************************  

use country_level_export_data.dta, clear

tostring commodity_code, gen(cmcode)
gen length=length(cmcode)
replace cmcode="0"+cmcode if length==7
drop length
drop commodity_code

ren trade_country_code cty_id
drop trade_country_name

gen ym=ym(year, month)	  
format ym %tm   
drop month
keep if ym>=tm(2017m1) & ym<=tm(2019m12)  
drop if cty_id==142 // drop "exports" to China
collapse (sum) value quantity (mean) year, by(ym cmcode cty_id)

gen cmcode6=substr(cmcode,1,6)
gen cmcode8=substr(cmcode,1,8) 
 
* Merge with 中美贸易战商品代码清单 
* 34 billion round: announced on June 15, 2018, enforced on July 6, 2018, tariff rate + 25% 
* 16 billion round: announced on Aug 7, 2018, enforced on Aug 23, 2018, tariff rate + 25%
* 200 billion round: announced on July 10, 2018, enforced on Sep 18, 2018, tariff rate + 10%  
* 200 billion round (May 2019): announced on May 8, 2019, enforced on May 10, 2019, tariff rate + 15% based on previous rates
* 300 billion round: announced on Aug, 2019, enforced on Sep 1, 2019, tariff rate + 15%  
 
**合并由10位生成的8位清单并手动处理同一个HS8下不同HS10处于不同round的情况
* 34 billion, 16 billion, 200 billion rounds
preserve
use trade_war_product_list_hs10.dta, clear
gen cmcode8=substr(cmcode,1,8)
keep 加征关税批次 cmcode8
duplicates drop
duplicates report cmcode8 
duplicates list cmcode8
   
browse if cmcode8=="84082010"
replace 加征关税批次="160亿商品" if cmcode8=="84082010"
browse if cmcode8=="84201000"
replace 加征关税批次="340亿商品" if cmcode8=="84201000"
browse if cmcode8=="84248999"
replace 加征关税批次="340亿商品" if cmcode8=="84248999"
browse if cmcode8=="85044099"
replace 加征关税批次="340亿商品" if cmcode8=="85044099"
browse if cmcode8=="85142000"
replace 加征关税批次="340亿商品" if cmcode8=="85142000"
browse if cmcode8=="85291090"
replace 加征关税批次="160亿商品" if cmcode8=="85291090"
browse if cmcode8=="85437099"
replace 加征关税批次="160亿商品" if cmcode8=="85437099"
 
duplicates drop

save trade_war_product_list_hs10_hs8.dta, replace 

restore 

* 300 billion round
preserve
use us_300_billion_list_allhs6.dta, clear
ren hs6_commodity_code cmcode6
gen 加征关税批次="3000亿商品"
save trade_war_300b_list.dta, replace 
restore 

merge m:1 cmcode8 using "trade_war_product_list_hs10_hs8.dta", update
drop if _merge==2 
drop _merge
erase trade_war_product_list_hs10_hs8.dta
  
**合并8位 前三批清单
merge m:1 cmcode8 using "trade_war_product_list_hs8_supplement.dta",update 
drop if _merge==2 
drop 序号-_merge

**合并6位 前三批清单
merge m:1 cmcode6 using "trade_war_product_list_hs6_supplement.dta",update
drop if _merge==2 
drop _merge 

* 合并3000亿清单
merge m:1 cmcode6 using "trade_war_300b_list.dta",update
drop if _merge==2 
drop _merge 
erase trade_war_300b_list.dta
 
gen tariff_round=0
replace tariff_round=34 if 加征关税批次=="340亿商品"
replace tariff_round=16 if 加征关税批次=="160亿商品"
replace tariff_round=200 if 加征关税批次=="2000亿商品"	
replace tariff_round=300 if 加征关税批次=="3000亿商品"	

drop 加征关税批次
   
* Merge UN Comtrade 6-digit tariff rates
merge m:1 cmcode6 cty_id year using import_tariff_6digit_fill.dta
drop if _merge==2
drop _merge  
replace tariff_rate=0 if tariff_rate==. // set the very few unmatched missing values of tariff rates as zero
  
* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==34 & ym>=tm(2018m7)  
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==16 & ym>=tm(2018m8)  
replace tariff_rate=tariff_rate+10 if cty_id==502 & tariff_round==200 & ym>=tm(2018m9)  
replace tariff_rate=tariff_rate+15 if cty_id==502 & tariff_round==200 & ym>=tm(2019m5)  
replace tariff_rate=tariff_rate+15 if cty_id==502 & tariff_round==300 & ym>=tm(2019m9)  
 
egen gt = group(cmcode8 ym) // product-time
egen ct = group(cty_id ym) // country-time
egen cg = group(cty_id cmcode8)
egen g  = group(cmcode8)

gen lnexport=ln(value)
gen ln_p=ln(value/quantity)
gen ln_q=ln(quantity)  
gen lnTariff=ln(1+tariff_rate/100)
 
 
/* Event-study analysis */
* The aim of this is to see the medium-run responses. 
egen g8=group(cmcode8)
 
* generate event time for each group
gen event_time=.
replace event_time=ym-tm(2018m7) if tariff_round==34 & cty_id==502
replace event_time=ym-tm(2018m8) if tariff_round==16 & cty_id==502 
replace event_time=ym-tm(2018m9) if tariff_round==200 & cty_id==502 
replace event_time=ym-tm(2019m9) if tariff_round==300 & cty_id==502  
replace event_time=-7 if event_time<=-7
replace event_time=13  if event_time>=13 & event_time!=.

foreach num of numlist 7(-1)1{
	gen treat_pre_`num'=0
	replace treat_pre_`num'=1 if event_time*(-1)==`num'  
}
foreach num of numlist 0(1)13{
	gen treat_post_`num'=0
	replace treat_post_`num'=1 if event_time==`num'  
} 

* Figure A5 * 
preserve 
* set omitted group
replace treat_pre_1=0
 
qui reghdfe lnexp treat_pre_* treat_post_*, a(gt ct cg) cluster(g8)
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_7="<-6" treat_pre_*="- " treat_post_13=">12" treat_post_*=" " ) ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle(ln(exports)) xtitle(month after tariff increase)  
graph export Fig-EventStudy-cgtlevel-national.eps, replace
  
qui reghdfe ln_p treat_pre_* treat_post_*, a(gt ct cg) cluster(g8)
estimates store event    
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_7="<-6" treat_pre_*="- " treat_post_13=">12" treat_post_*=" " ) ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle(ln(export price/unit)) xtitle(month after tariff increase)   
graph export Fig-EventStudy-cg_price-national.eps, replace   
  
qui reghdfe ln_q treat_pre_* treat_post_*, a(gt ct cg) cluster(g8)
estimates store event    
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_7="<-6" treat_pre_*="- " treat_post_13=">12" treat_post_*=" " ) ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle(ln(export quantity)) xtitle(month after tariff increase)  
graph export Fig-EventStudy-cg_q-national.eps, replace  
restore 
  
* Quantitative analysis of tariff rate and exports
* Table A6 *  
* 2017m1-2019m4 *
preserve
keep if ym<=tm(2019m4) 
qui reghdfe lnexport lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label replace 
qui reghdfe ln_p lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append 
qui reghdfe ln_q lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append  
restore
 
* 2017m1-2019m12 *
qui reghdfe lnexport lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append 
qui reghdfe ln_p lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append 
qui reghdfe ln_q lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append  


   